<!--车辆管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">车辆维修</span>
    <el-button @click="beforeAddRepairVehicle" type="primary"
               style="float:right;margin-top:13px;">录入维修车辆
    </el-button>
  </div>
  <!-- 新增车辆弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible"
             style="width:1000px;padding:40px;" :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="送修人">
            <el-input placeholder="请输入内容" v-model="saveRepairVehicleForm.username" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修车辆">
            <el-select placeholder="请选择" v-model="saveRepairVehicleForm.vehicleId">
              <el-option v-for="item in vehicleList" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修项目">
            <el-select placeholder="请选择" v-model="saveRepairVehicleForm.item">
              <el-option v-for="item in repairItemArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修地点">
            <el-select placeholder="请选择" v-model="saveRepairVehicleForm.addr">
              <el-option v-for="item in repairAddrArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修材料">
            <el-select placeholder="请选择" v-model="saveRepairVehicleForm.material">
              <el-option v-for="item in repairMaterialArr" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修费用">
            <el-input placeholder="请输入" v-model="saveRepairVehicleForm.fee"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="30">
        <el-col :span="12">
          <el-form-item label="维修时间">
            <el-date-picker
                v-model="saveRepairVehicleForm.repairTime"
                type="date"
                placeholder="请选择"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="维修状态">
            <el-radio-group v-model="saveRepairVehicleForm.status">
              <el-radio v-for="item in repairStatusArr" :label="item.label" :value="item.value"></el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveRepairVehicle">确定</el-button>
    </template>
  </el-dialog>
  <!-- 搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="维修车辆">
        <el-input placeholder="请输入车牌号" style="width:220px;"
                  v-model="searchRepairForm.license" @keydown.enter="loadRepairVehicle"></el-input>
      </el-form-item>
      <el-form-item label="送修人员" style="width:290px;">
        <el-input placeholder="请输入" style="width:220px;"
                  v-model="searchRepairForm.username" @keydown.enter="loadRepairVehicle"></el-input>
      </el-form-item>
      <el-form-item label="维修状态" style="width:290px;">
        <el-select placeholder="请选择" v-model="searchRepairForm.status" @select="loadRepairVehicle"
                   style="width:220px;">
          <el-option v-for="item in repairStatusArr" :label="item.label" :value="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="primary" @click="loadRepairVehicle">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 车辆列表 -->
  <el-card style="margin:20px;">
    <el-table :data="repairVehicleArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="license" label="维修车辆" align="center"/>
      <el-table-column prop="item" label="维修项目" align="center"
                       :formatter="repairItemFormatter"/>
      <el-table-column prop="username" label="送修人员" align="center"/>
      <el-table-column prop="addr" label="维修地点" align="center"
                       :formatter="repairAddrFormatter"/>
      <el-table-column prop="repairTime" label="维修时间" align="center"/>
      <el-table-column prop="fee" label="维修费用" align="center"/>
      <el-table-column prop="material" label="维修材料" align="center"
                       :formatter="repairMaterialFormatter"/>
      <el-table-column prop="status" label="维修状态" align="center"
                       :formatter="repairStatusFormatter"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small"
                     @click="editRepairVehicle(scope.row.id)">编辑
          </el-button>
          <el-button link type="primary" size="small" @click="deleteRepairVehicle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

//控制新增车辆弹窗是否显示
const dialogVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('');

//定义对象用来保存车辆搜索条件 品牌 车牌号
const searchRepairForm = ref({license: '', username: '', status: ''});
//定义数组用来保存车辆表格数据
const repairVehicleArr = ref([]);
//定义加载车辆的方法
const loadRepairVehicle = () => {
  console.log(searchRepairForm.value);
  let data = qs.stringify(searchRepairForm.value);
  axios.get(BASE_URL + '/v1/repairVehicle/select?' + data).then((response) => {
    if (response.data.code == 2000) {
      repairVehicleArr.value = response.data.data;
      console.log(repairVehicleArr.value);
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义数组用来保存请求回来的字典项
const repairStatusArr = ref([]);
const repairMaterialArr = ref([]);
const repairItemArr = ref([]);
const repairAddrArr = ref([]);
const vehicleList = ref([]);

//onMounted() VUE的生命周期钩子 在DOM渲染完毕后立即执行
onMounted(() => {
  loadRepairVehicle();
  loadDictOptions(repairStatusArr, 'repair_status');
  loadDictOptions(repairMaterialArr, 'repair_material');
  loadDictOptions(repairItemArr, 'repair_item');
  loadDictOptions(repairAddrArr, 'repair_addr');
})
//定义搜索卡片重置按钮的功能 清空值+重新加载车辆
const resetSearch = () => {
  searchRepairForm.value = {};
  loadRepairVehicle();
}

const repairStatusFormatter = (row, column, cellValue, index) => {
  if (!repairStatusArr.value) {
    return cellValue;
  }
  for (let item of repairStatusArr.value) {
    if (item.value == cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
}
const repairMaterialFormatter = (row, column, cellValue, index) => {
  if (!repairMaterialArr.value) {
    return cellValue;
  }
  for (let item of repairMaterialArr.value) {
    if (item.value == cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
}
const repairItemFormatter = (row, column, cellValue, index) => {
  if (!repairItemArr.value) {
    return cellValue;
  }
  for (let item of repairItemArr.value) {
    if (item.value == cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
}
const repairAddrFormatter = (row, column, cellValue, index) => {
  if (!repairAddrArr.value) {
    return cellValue;
  }
  for (let item of repairAddrArr.value) {
    if (item.value == cellValue) {
      cellValue = item.label;
    }
  }
  return cellValue;
}

//定义对象用来收集维修车辆弹窗表单的所有数据
const saveRepairVehicleForm = ref({
  id: '',
  username: getUser().username,
  userId: getUser().id,
  item: '',
  material: '',
  repairTime: '',
  vehicleId: '',
  addr: '',
  fee: '',
  status: '',
})
//加载车辆列表
const loadVehicleList = () => {
  axios.get(BASE_URL + '/v1/vehicle/select').then((response) => {
    if (response.data.code == 2000) {
      for (let i = 0; i < response.data.data.length; i++) {
        vehicleList.value.push({label: response.data.data[i].license, value: response.data.data[i].id})
      }
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//点击"新建车辆"按钮,调用此函数
const beforeAddRepairVehicle = () => {
  //确保每次新增时的标题,且弹窗表单中没有数据
  dialogTitle.value = '录入维修信息';
  dialogVisible.value = true;
  loadVehicleList();
  saveRepairVehicleForm.value = {username: getUser().username, userId: getUser().id};
}

//定义保存车辆的方法
const saveRepairVehicle = () => {
  console.log(saveRepairVehicleForm.value);
  //把收集到的表单数据转为查询字符串格式
  let data = qs.stringify(saveRepairVehicleForm.value);
  //给后端发请求
  axios.post(BASE_URL + '/v1/repairVehicle/save', data).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('保存成功!');
      //保存成功后,需要清空表单数据,关闭弹窗,并重新加载车辆数据
      saveRepairVehicleForm.value = {username: getUser().username, userId: getUser().id};
      dialogVisible.value = false;
      loadRepairVehicle();
    } else {
      //数据库里的code与license字段设置了唯一
      //如果出现重复值会操作失败,所以我们可以设置操作失败的提示语句
      ElMessage.error(response.data.msg);
    }
  })
}

//处理保存车辆弹窗关闭的方法
const handleClose = () => {
  if (confirm('您确认要关闭本弹窗吗?')) {
    dialogVisible.value = false;
  }
}

//定义编辑车辆的方法
const editRepairVehicle = (id) => {
  //1.显示弹窗+修改弹窗标题
  dialogVisible.value = true;
  dialogTitle.value = '编辑维修车辆信息';
  loadVehicleList();
  //2.根据id查询当前指定车辆的详细数据
  axios.get(BASE_URL + '/v1/repairVehicle/select?id=' + id).then((response) => {
    if (response.data.code == 2000) {
      //3.将数据赋值给表单响应式变量,注意!后端返回的是一个List<VehicleVO>
      saveRepairVehicleForm.value = response.data.data[0];
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

//删除指定车辆
const deleteRepairVehicle = (id) => {
  if (confirm('您确认要删除此维修信息吗?')) {
    axios.post(BASE_URL + '/v1/repairVehicle/delete/' + id).then((response) => {
      if (response.data.code == 2000) {
        ElMessage.success('删除成功!');
        loadRepairVehicle();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}
</script>